﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImagesViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //====================================================================================================================
        private int screenHeight = 0;
        //====================================================================================================================
        private void showFile(string fileName)
        {
            try
            {
                if(0==screenHeight)
                {
                    screenHeight = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
                ExifUtils.Exif.GetExifData(files[currentImage]);
                //
                BitmapImage myBitmapImage = new BitmapImage();
                // BitmapImage.UriSource must be in a BeginInit/EndInit block
                myBitmapImage.BeginInit();
                myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                myBitmapImage.UriSource = new Uri(fileName);//, new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore));
                if (ExifUtils.Exif.IsRotation && (90 == Math.Abs(ExifUtils.Exif.Rotation)))
                {
                    //obrócony obraz bedzie o 90 deg
                    myBitmapImage.DecodePixelWidth = screenHeight;
                }
                else
                {
                    myBitmapImage.DecodePixelHeight = screenHeight;
                }
                myBitmapImage.EndInit();

                // To save significant application memory, set the DecodePixelWidth or   
                // DecodePixelHeight of the BitmapImage value of the image source to the desired  
                // height or width of the rendered image. If you don't do this, the application will  
                // cache the image as though it were rendered as its normal size rather then just  
                // the size that is displayed. 
                // Note: In order to preserve aspect ratio, set DecodePixelWidth 
                // or DecodePixelHeight but not both.
                if (ExifUtils.Exif.IsRotation && (0 != ExifUtils.Exif.Rotation))
                {
                    TransformedBitmap tb = new TransformedBitmap();
                    tb.BeginInit();
                    tb.Source = myBitmapImage;
                    tb.Transform = new RotateTransform(ExifUtils.Exif.Rotation);
                    tb.EndInit();
                    // Set the Image source.
                    image1.Source = tb;
                }
                else
                {
                    image1.Source = myBitmapImage;
                }
                //set image source
                //GC.Collect(0); 
                myBitmapImage.UriSource = null;
                myBitmapImage = null;
                
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message, "showFile");
            }

        }
         //====================================================================================================================
        private void infoClean()
        {
            win1.Title="";
            tb1.Text="";
            tb1.ToolTip = "";
        }
        private void infoShow(string filePathName)
        {
            string fileName = System.IO.Path.GetFileNameWithoutExtension(filePathName);
            win1.Title = System.IO.Path.GetFileName(filePathName);
            tb1.Text=fileName;
            tb1.ToolTip = filePathName;
        }
         //====================================================================================================================
        private const string TrashBinName = ".Deleted";
        private void deleteFileSafe(string pathname)
        {
            string targetPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pathname), TrashBinName);
            try
            {
                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }
                string targetName = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(pathname));
                if (System.IO.File.Exists(targetName)) System.IO.File.Delete(targetName);
                System.IO.File.Move(pathname, targetName);
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("[{0}]\n{1}", pathname, e.Message), "deleteFileSafe", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        //====================================================================================================================
        private void showNext()
        {
            if (0 == files.Count()) return;
            if ((currentImage + 1) >= files.Count()) return;
            //
            currentImage++;
            showFile(files[currentImage]);
            infoShow(files[currentImage]);

        }
        private void showPrev()
        {
            if (0 == files.Count()) return;
            if (currentImage <= 0) return;
            //
            currentImage--;
            showFile(files[currentImage]);
            infoShow(files[currentImage]);

        }
        private void showLast()
        {
            if (0 == files.Count()) return;
            if ((currentImage + 1) >= files.Count()) return;
            //
            currentImage = files.Count() - 1;
            showFile(files[currentImage]);
            infoShow(files[currentImage]);

        }
        private void showFirst()
        {
            if (0 == files.Count()) return;
            if (currentImage <= 0) return;
            //
            currentImage=0;
            showFile(files[currentImage]);
            infoShow(files[currentImage]);

        }
        //====================================================================================================================
        private void deleteCurrent()
        {
            if (0 == files.Count()) return;
            //
            if (1 == files.Count())
            {
                infoClean();
                image1.Source = null;
                files.RemoveAt(0);
                return;
            }
            //
            if ((currentImage + 1) == files.Count())//last
            {
                deleteFileSafe(files[currentImage]);
                files.RemoveAt(currentImage);
                currentImage--;
                showFile(files[currentImage]);
                infoShow(files[currentImage]);
                return;
            }
            else
            {
                deleteFileSafe(files[currentImage]);
                files.RemoveAt(currentImage);
                showFile(files[currentImage]);
                infoShow(files[currentImage]);
            }
        }
        //====================================================================================================================
    }
}
